package com.example.service.impl;

import com.example.mapper.EmpExprMapper;
import com.example.mapper.EmpMapper;
import com.example.pojo.*;
import com.example.service.EmpService;
import com.example.utils.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    //批量删除员工基本信息和工作经历
    @Transactional
    @Override
    public void delete(List<Integer> ids) {
        //1.批量删除员工基本信息
        empMapper.deleteById(ids);
        //2.批量删除员工工作经历信息
        empExprMapper.deleteByEmpId(ids);
    }
    //条件分页查询员工信息
    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
//        Long total = empMapper.count();
//        Integer start=  (page-1)*pageSize;
//        List<Emp> list = empMapper.list(start, pageSize);
//        return new PageResult<>(total,list);
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        List<Emp> list = empMapper.list(empQueryParam);
        Page<Emp> p=(Page<Emp>) list;
        return new PageResult<Emp>(p.getTotal(),p.getResult());
    }
    //增加员工基本信息和工作经历
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void sava(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);

        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
        }
        empExprMapper.insertBatch(exprList);
    }
    //根据id查询员工基本信息和工作经历
    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getInfo(id);
    }
    //修改员工基本信息和工作经历
    @Override
    public void update(Emp emp) {
        //1.修改员工基本信息
        empMapper.updateById(emp);
        //2.修改员工工作经历信息
        //2.1删除员工工作经历信息
        empExprMapper.deleteByEmpId(Arrays.asList(emp.getId()));
        //2.2添加员工工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
            empExprMapper.insertBatch(exprList);
        }
    }
    //员工登录
    @Override
    public LoginInfo login(Emp emp) {
        Emp empLogin = empMapper.getUsernameAndPassword(emp);
        if (empLogin!=null){
            //1.生成Jwt令牌
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id",empLogin.getId());
            dataMap.put("username",empLogin.getUsername());
            String jwt = JwtUtils.generateJwt(dataMap);
            LoginInfo loginInfo = new LoginInfo(empLogin.getId(), empLogin.getUsername(), empLogin.getName(), jwt);
            return loginInfo;
        }else {
            return null;
        }
    }


}
